/*
 * GenerarBaseThread.java
 *
 * Created on 29 de marzo de 2007, 04:39 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.src.computec.radar.consultas.thread;

import com.src.computec.radar.base.dao.exception.DAOException;
import com.src.computec.radar.consultas.base.constantes.ConstantesGenerales;
import com.src.computec.radar.consultas.generarbase.dao.BaseGenerarBaseDAO;
import com.src.log4j.logger.custom.XLogger;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author aocampo
 */
public class GenerarBaseThread implements Runnable {
    
    private static XLogger xLogger;
    private BaseGenerarBaseDAO dao;
    private List listNumerosOrdenes;
    private boolean conInsertos;
    private boolean conTipoDestinoCliente;
    private String ldapUser;
    
    /**
     * Creates a new instance of GenerarBaseThread 
     */
    public GenerarBaseThread(BaseGenerarBaseDAO dao, List listNumerosOrdenes, boolean conInsertos, boolean conTipoDestinoCliente, String ldapUser) {
        this.xLogger = (XLogger) XLogger.getLogger(this.getClass().getName());
        this.dao = dao;
        this.listNumerosOrdenes = listNumerosOrdenes;
        this.conInsertos = conInsertos;
        this.conTipoDestinoCliente = conTipoDestinoCliente;
        this.ldapUser = ldapUser;
    }
    
    public void run() {
        try {
            Iterator iterator = listNumerosOrdenes.iterator();
            while (iterator.hasNext()) {
                String numeroOrden = (String) iterator.next();
                xLogger.info("GenerarBaseThread:run:ldapUser:" + ldapUser + ":numeroOrden:" + numeroOrden);
                List listBase = dao.getBaseByNumeroOrden(numeroOrden, conInsertos, conTipoDestinoCliente);
                String fileName = ConstantesGenerales.RANDOM_STRING_UTILS.randomAlphanumeric(20) + ConstantesGenerales.FILE_EXTENSION;
                Thread thread = new Thread(new FileThread(FileThread.FILE_BASE, fileName, listBase, conInsertos, conTipoDestinoCliente));
                thread.start();
                dao.insertMensajeAlerta(fileName, ldapUser, listBase.size(), numeroOrden);
                xLogger.info("GenerarBaseThread:run:ldapUser:" + ldapUser + ":numeroOrden:" + numeroOrden);
            }
        }
        catch (DAOException e) {
            xLogger.lethal("GenerarBaseThread:run:", e);
        }
    }
}